﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;
using YiSha.Model.Result;
using YiSha.Web.Code;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-03-19 19:39
    /// 描 述：商铺分类业务类
    /// </summary>
    public class GoodsCategoryBLL
    {
        private GoodsCategoryService goodsCategoryService = new GoodsCategoryService();

        #region 获取数据
        public async Task<TData<List<GoodsCategoryEntity>>> GetList(GoodsCategoryListParam param)
        {
            TData<List<GoodsCategoryEntity>> obj = new TData<List<GoodsCategoryEntity>>();
            obj.Data = await goodsCategoryService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<GoodsCategoryEntity>>> GetPageList(GoodsCategoryListParam param, Pagination pagination)
        {
            TData<List<GoodsCategoryEntity>> obj = new TData<List<GoodsCategoryEntity>>();
            obj.Data = await goodsCategoryService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<ZtreeInfo>>> GetCategoryTreeListJson(GoodsCategoryListParam param)
        {
            var obj = new TData<List<ZtreeInfo>>();
            obj.Data = new List<ZtreeInfo>();
            List<GoodsCategoryEntity> departmentList = await goodsCategoryService.GetList(param);
            OperatorInfo operatorInfo = await Operator.Instance.Current();
            if (operatorInfo.IsSystem != 1)
            {
                List<long> childrenDepartmentIdList = await GetChildrenCategoryIdList(departmentList, operatorInfo.DepartmentId.Value);
                departmentList = departmentList.Where(p => childrenDepartmentIdList.Contains(p.Id.Value)).ToList();
            }
            foreach (GoodsCategoryEntity department in departmentList)
            {
                obj.Data.Add(new ZtreeInfo
                {
                    id = department.Id,
                    pId = department.parent_id,
                    name = department.category_name
                });
            }
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<GoodsCategoryEntity>> GetEntity(long id)
        {
            TData<GoodsCategoryEntity> obj = new TData<GoodsCategoryEntity>();
            obj.Data = await goodsCategoryService.GetEntity(id);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 获取当前部门及下面所有的部门
        /// </summary>
        /// <param name="departmentList"></param>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public async Task<List<long>> GetChildrenCategoryIdList(List<GoodsCategoryEntity> categoryList, long categoryId)
        {
            if (categoryList == null)
            {
                categoryList = await goodsCategoryService.GetList(null);
            }
            List<long> departmentIdList = new List<long>();
            departmentIdList.Add(categoryId);
            GetChildrenCategoryIdList(categoryList, categoryId, departmentIdList);
            return departmentIdList;
        }
        #endregion 
        #region 提交数据
        public async Task<TData<string>> SaveForm(GoodsCategoryEntity entity)
        {
            TData<string> obj = new TData<string>();
            await goodsCategoryService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await goodsCategoryService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 获取该部门下面所有的子部门
        /// </summary>
        /// <param name="departmentList"></param>
        /// <param name="departmentId"></param>
        /// <param name="departmentIdList"></param>
        private void GetChildrenCategoryIdList(List<GoodsCategoryEntity> departmentList, long categoryId, List<long> categoryIdIdList)
        {
            var children = departmentList.Where(p => p.parent_id == categoryId).Select(p => p.Id.Value).ToList();
            if (children.Count > 0)
            {
                categoryIdIdList.AddRange(children);
                foreach (long id in children)
                {
                    GetChildrenCategoryIdList(departmentList, id, categoryIdIdList);
                }
            }
        }
        #endregion
    }
}
